﻿#region License
//
// Author: Pål Fossmo <pal@fossmo.net>
// Copyright (c) 2008 Pål Fossmo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
using System.IO;
using System.Text;

namespace FileLibrary
{
    /// <summary>
    /// Exposes methods for renaming directories and replace strings in files.
    /// </summary>
    public static class Rename
    {
        /// <summary>
        /// Recursively renames all sub directories of the given working directory.
        /// </summary>
        /// <param name="workingDirectory">The start directory of the operation.</param>
        /// <param name="directoryToReplace">A <see cref="String"/> to be replaced.</param>
        /// <param name="newDirectory">A <see cref="String"/> to replace all occurences of <see cref="directoryToReplace"/>.</param>
        public static void Directories(string workingDirectory, string directoryToReplace, string newDirectory)
        {
            bool isReadyToExit = false;

            while (!isReadyToExit)
            {
                isReadyToExit = true;
                foreach (string dir in Directory.GetDirectories(workingDirectory, "*.*", SearchOption.AllDirectories))
                {
                    if (dir.IndexOf(directoryToReplace) > 0)
                    {
                        Directory.Move(dir, dir.Replace(directoryToReplace, newDirectory));
                        isReadyToExit = false;
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// Replaces all occurences of a specified <see cref="String"/> in a file, with another specified <see cref="String"/>.
        /// </summary>
        /// <param name="file">The file to perform the operation on.</param>
        /// <param name="textToReplace">A <see cref="String"/> to be replaced.</param>
        /// <param name="newText">A <see cref="String"/> to replace all occurences of <see cref="textToReplace"/>.</param>
        public static void TextInFile(string file, string textToReplace, string newText)
        {
            Encoding encoding = Encoding.UTF8;
            StreamReader sr = new StreamReader(file, encoding);
            StreamWriter sw = new StreamWriter(file + "_work", false, encoding);
            while (sr.Peek() > -1)
            {
                string line = sr.ReadLine();
                line = line.Replace(textToReplace, newText);
                sw.WriteLine(line);
            }
            sr.Close();
            sw.Close();
            File.Delete(file);
            File.Copy(file + "_work", file.Replace(textToReplace, newText));
            File.Delete(file + "_work");
        }
    }
}
